package com.ruoyi.web.controller.blog.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.TbBlog;
import com.ruoyi.blog.service.ITbBlogService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文章Controller
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/ruoyi/blog")
public class TbBlogController extends BaseController {
    @Autowired
    private ITbBlogService tbBlogService;

    @PreAuthorize("@ss.hasPermi('ruoyi:blog:upload')")
    @GetMapping("/upload")
    public R<String> upload(MultipartFile file) {
        try {
            String upload = FileUploadUtils.upload(file);
            return R.ok(upload);
        } catch (IOException e) {
            return R.ok("上传失败");
        }
    }

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBlog tbBlog) {
        startPage();
        List<TbBlog> list = tbBlogService.selectTbBlogList(tbBlog);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:export')")
    @Log(title = "文章导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBlog tbBlog) {
        List<TbBlog> list = tbBlogService.selectTbBlogList(tbBlog);
        ExcelUtil<TbBlog> util = new ExcelUtil<TbBlog>(TbBlog.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:query')")
    @GetMapping(value = "/{blogId}")
    public AjaxResult getInfo(@PathVariable("blogId") String blogId) {
        return success(tbBlogService.selectTbBlogByBlogId(blogId));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:add')")
    @Log(title = "新增文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBlog tbBlog) {
        return toAjax(tbBlogService.insertTbBlog(tbBlog));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:edit')")
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBlog tbBlog) {
        return toAjax(tbBlogService.updateTbBlog(tbBlog));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:blog:remove')")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{blogIds}")
    public AjaxResult remove(@PathVariable String[] blogIds) {
        return toAjax(tbBlogService.deleteTbBlogByBlogIds(blogIds));
    }
}
